package com.eduask.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.eduask.beans.CarSchema;
import com.eduask.dao.CarSchemaDAO;
import com.eduask.util.HibernateUtil;

@Repository
public class CarSchemaDAOImpl extends HibernateUtil implements CarSchemaDAO {

	@Override
	public CarSchema findCarSchemaById(Integer id) {
		
		return (CarSchema) getSession().createQuery("from CarSchema where id = ?").setParameter(0, id).uniqueResult();
	}

	@Override
	public List<CarSchema> findAllCarSchemas() {

		return getSession().createQuery("from CarSchema carSchema order by carSchema.id asc").list();
	}

	@Override
	public void updateCarSchema(CarSchema carSchema) {
		getSession().saveOrUpdate(carSchema);
	}

	@Override
	public List<CarSchema> findCarSchemas(CarSchema carSchema) {
		String hql = "from CarSchema carSchema where carSchema.startStation= ? "
				+ "and carSchema.finalStation = ? "
				+ "and carSchema.startTime = ? "
				+ "and carSchema.clock >= ?"
				+ "and carSchema.clock <= ?";
		Query query = getSession().createQuery(hql);
		query.setParameter(0, carSchema.getFinalStation());
		query.setParameter(1, carSchema.getStartTime());
		query.setParameter(2, carSchema.getClock());
		query.setParameter(3, carSchema.getClock());
		return query.list();
	}

	@Override
	public List<String> getStartplace() {
		// TODO Auto-generated method stub
		String hql = "select distinct c.startStation from CarSchema c";
		List<String> list = getSession().createQuery(hql).list();
		
		return list;
	}

	@Override
	public List<String> getEndplace(String startPlace) {
		// TODO Auto-generated method stub
		String hql = "select distinct c.finalStation from CarSchema c where c.startStation=?";
		
		List<String> list = getSession().createQuery(hql).setParameter(0, startPlace).list();
		return list;
	}

	@Override
	public List<CarSchema> getCar(String start, String endplace,String time) {
		// TODO Auto-generated method stub
		String hql = "from CarSchema c where c.startStation=? and c.finalStation=? and c.startTime like '%"+time.substring(0,10)+"%'";
		List<CarSchema> list = getSession().createQuery(hql).setParameter(0, start)
				.setParameter(1, endplace).list();
	
		return list;
	}
}
